package com.zlp.security.dto;

import com.zlp.entity.User;
import com.zlp.enums.StatusEnum;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Data
public class MyUserDetails implements UserDetails {

    /**
     * 用户信息
     */
    protected User user;

    private List<GrantedAuthority> grantedAuthorities;

    public MyUserDetails(User umsAdmin, List<GrantedAuthority> grantedAuthorities) {
        this.user = umsAdmin;
        this.grantedAuthorities = grantedAuthorities;
    }

    /**
     * 装配权限
     *
     * @Date 2020-6-17 17:45
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        return grantedAuthorities;
    }

    @Override
    public String getPassword() {

        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return Objects.isNull(user) ? "" : user.getUsername();
    }

    /**
     * 账号是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return Boolean.TRUE;
    }

    /**
     * 账号是否正常
     */
    @Override
    public boolean isAccountNonLocked() {
        return Boolean.TRUE;
    }

    /**
     * 是否授权没有过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return Boolean.TRUE;
    }

    /**
     * 是否启用
     */
    @Override
    public boolean isEnabled() {
        return user.getStatus() == StatusEnum.NORMAL.getValue() ? Boolean.TRUE : Boolean.FALSE;
    }
}
